//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================
// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy
//===============================================================================

using System;
using System.Diagnostics;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using EntLibContrib.Logging.TraceListeners;

namespace EntLibContrib.Logging.Configuration
{
	/// <summary>
	/// Represents the configuration data for a <see cref="NoGuidRollingFlatFileTraceListenerData"/>.
	/// </summary>
	public class NoGuidRollingFlatFileTraceListenerData : TraceListenerData, IFileTraceListenerData
	{
		#region Constants
		private const string FileNamePropertyName = "fileName";
		#endregion

		#region Properties
		/// <summary>
		/// FileName
		/// </summary>
		/// <value></value>
		[ConfigurationProperty(FileNamePropertyName)]
		public string FileName
		{
			get { return (string)this[FileNamePropertyName]; }
			set { this[FileNamePropertyName] = value; }
		}

		private const string MaxInstancesPropertyName = "maxInstances";
		/// <summary>
		/// Max Instances in for loop appending count to file name, where 0 is no max
		/// </summary>
		/// <value>The max instances.</value>
		[ConfigurationProperty(MaxInstancesPropertyName, DefaultValue = (byte)0)]
		public byte MaxInstances
		{
			get { return (byte)this[MaxInstancesPropertyName]; }
			set { this[MaxInstancesPropertyName] = value; }
		}

		private const string ThrowMaxPropertyName = "throwMax";
		/// <summary>
		/// Throw Exception if Max Instances reached
		/// </summary>
		/// <value><c>true</c> if [throw max]; otherwise, <c>false</c>.</value>
		[ConfigurationProperty(ThrowMaxPropertyName)]
		public bool ThrowMax
		{
			get { return (bool)this[ThrowMaxPropertyName]; }
			set { this[ThrowMaxPropertyName] = value; }
		}

		private const string RollSizeKBPropertyName = "rollSizeKB";
		/// <summary>
		/// Roll Size KB
		/// </summary>
		/// <value>The roll size KB.</value>
		[ConfigurationProperty(RollSizeKBPropertyName)]
		public int RollSizeKB
		{
			get { return (int)this[RollSizeKBPropertyName]; }
			set { this[RollSizeKBPropertyName] = value; }
		}

		private const string TimeStampPatternPropertyName = "timeStampPattern";
		/// <summary>
		/// Time stamp
		/// </summary>
		/// <value>The time stamp pattern.</value>
		[ConfigurationProperty(TimeStampPatternPropertyName)]
		public string TimeStampPattern
		{
			get { return (string)this[TimeStampPatternPropertyName]; }
			set { this[TimeStampPatternPropertyName] = value; }
		}

		private const string RollFileExistsBehaviorPropertyName = "rollFileExistsBehavior";
		/// <summary>
		/// Exists Behavior
		/// </summary>
		/// <value>The roll file exists behavior.</value>
		[ConfigurationProperty(RollFileExistsBehaviorPropertyName)]
		public NoGuidRollFileExistsBehavior RollFileExistsBehavior
		{
			get { return (NoGuidRollFileExistsBehavior)this[RollFileExistsBehaviorPropertyName]; }
			set { this[RollFileExistsBehaviorPropertyName] = value; }
		}

		private const string RollIntervalPropertyName = "rollInterval";
		/// <summary>
		/// Roll Intervall
		/// </summary>
		/// <value>The roll interval.</value>
		[ConfigurationProperty(RollIntervalPropertyName)]
		public RollInterval RollInterval
		{
			get { return (RollInterval)this[RollIntervalPropertyName]; }
			set { this[RollIntervalPropertyName] = value; }
		}

		private const string RevolvingDaysPropertyName = "revolvingDays";
		/// <summary>
		/// Revolving Days to increment before overwrite.
		/// </summary>
		/// <value>The revolving days.</value>
		[ConfigurationProperty(RevolvingDaysPropertyName)]
		public double RevolvingDays
		{
			get { return (double)this[RevolvingDaysPropertyName]; }
			set { this[RevolvingDaysPropertyName] = value; }
		}

		private const string formatterNameProperty = "formatter";
		/// <summary>
		/// Gets and sets the formatter name.
		/// </summary>
		/// <value></value>
		[ConfigurationProperty(formatterNameProperty, IsRequired = false)]
		public string Formatter
		{
			get { return (string)base[formatterNameProperty]; }
			set { base[formatterNameProperty] = value; }
		}

		private const string headerProperty = "header";
		/// <summary>
		/// Gets and sets the header.
		/// </summary>
		/// <value></value>
		[ConfigurationProperty(headerProperty, IsRequired = false)]
		public string Header
		{
			get { return (string)base[headerProperty]; }
			set { base[headerProperty] = value; }
		}

		private const string footerProperty = "footer";
		/// <summary>
		/// Gets and sets the footer.
		/// </summary>
		/// <value></value>
		[ConfigurationProperty(footerProperty, IsRequired = false)]
		public string Footer
		{
			get { return (string)base[footerProperty]; }
			set { base[footerProperty] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="NoGuidRollingFlatFileTraceListenerData"/> class.
		/// </summary>
		public NoGuidRollingFlatFileTraceListenerData()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="NoGuidRollingFlatFileTraceListenerData"/> class.
		/// </summary>
		/// <param name="name">The name for the configuration object.</param>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="header">The header.</param>
		/// <param name="footer">The footer.</param>
		/// <param name="maxInstances">The max instances.</param>
		/// <param name="throwMax">if set to <c>true</c> [throw max].</param>
		/// <param name="rollSizeKB">The roll size KB.</param>
		/// <param name="timeStampPattern">The time stamp pattern.</param>
		/// <param name="rollFileExistsBehavior">The roll file exists behavior.</param>
		/// <param name="rollInterval">The roll interval.</param>
		/// <param name="revolvingDays">The revolving days to increment before overwrite.</param>
		/// <param name="traceOutputOptions">The trace options.</param>
		/// <param name="formatter">The formatter.</param>
		public NoGuidRollingFlatFileTraceListenerData(string name,
			string fileName,
			string header,
			string footer,
			byte maxInstances,
			bool throwMax,
			int rollSizeKB,
			string timeStampPattern,
			NoGuidRollFileExistsBehavior rollFileExistsBehavior,
			RollInterval rollInterval,
			double revolvingDays,
			TraceOptions traceOutputOptions,
			string formatter)
			: base(name, typeof(NoGuidRollingFlatFileTraceListener), traceOutputOptions)
		{
			this.FileName = fileName;
			this.Header = header;
			this.Footer = footer;
			this.MaxInstances = maxInstances;
			this.ThrowMax = throwMax;
			this.RollSizeKB = rollSizeKB;
			this.RollFileExistsBehavior = rollFileExistsBehavior;
			this.RollInterval = rollInterval;
			this.RevolvingDays = revolvingDays;
			this.TimeStampPattern = timeStampPattern;
			this.Formatter = formatter;
		}
        /// <summary>
        /// Returns a lambda expression that represents the creation of the trace listener described by this
        /// configuration object.
        /// </summary>
        /// <returns>A lambda expression to create a trace listener.</returns>
        protected override System.Linq.Expressions.Expression<Func<TraceListener>> GetCreationExpression()
        {
            return () =>
                new NoGuidRollingFlatFileTraceListener(
                    FileName,
                    Header,
                    Footer,
                    Container.ResolvedIfNotNull<ILogFormatter>(this.Formatter),
                    MaxInstances,
                    ThrowMax,
                    RollSizeKB,
                    TimeStampPattern,
                    RollFileExistsBehavior,
                    RollInterval,
                    RevolvingDays
                    );
		}

		#endregion
	}
}
